/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.beans;

import ProcesamientoImagenes.interfaces.BufferedImageMaker;
import candidate.ejb.VideoProcesingEjb;
import facades.CandidateEducationalInfoFacade;
import facades.CandidateFacade;
import facades.CandidateInterestAreaFacade;
import facades.CandidateLanguageInfoFacade;
import facades.CandidateProfessionalInfoFacade;
import facades.CandidateRelocatingInfoFacade;
import entities.Candidate;
import entities.CandidateEducationalInfo;
import entities.CandidateInterestArea;
import entities.CandidateLanguageInfo;
import entities.CandidateProfessionalInfo;
import entities.CandidateRelocatingInfo;
import util.ImageStoreHelper;
import util.PrimeFacesBufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.enterprise.event.Event;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import entities.CandidateBookImage;
import entities.CandidateTags;
import entities.Country;
import entities.LanguageLevel;
import entities.Province;
import facades.CandidateBookImageFacade;
import facades.CandidateTagsFacade;
import facades.LanguageLevelFacade;
import imageProcessing.classes.GeneralBufferedImageMaker;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.event.ComponentSystemEvent;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.RollbackException;
import javax.transaction.Transactional;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.UploadedFile;
import qualifiers.AdminCandidateEvent;
import qualifiers.EditedEntityEvent;
import util.FacesMessageHelper;

/**
 *
 * @author MIsabel
 */
@Named(value = "adminEditCandidateBean")
@SessionScoped
public class AdminEditCandidateBean implements Serializable {
    @Inject
    CandidateFacade candidateEJB;
    @Inject
    CandidateEducationalInfoFacade candidateEducationalInfoEJB;
    @Inject
    CandidateProfessionalInfoFacade candidateProfessionalInfoEJB;
    @Inject
    CandidateLanguageInfoFacade candidateLanguageInfoEJB;
    @Inject
    CandidateInterestAreaFacade candidateInterestAreaEJB;
    @Inject
    CandidateRelocatingInfoFacade candidateRelocatingInfoEJB;
    @Inject
    @AdminCandidateEvent
    private Event<Candidate> adminCandidateEvent;
    private Candidate candidate;
    @Inject
    private VideoProcesingEjb videoProcesingEjb;
    @Inject 
    private CandidateTagsFacade candidateTagsFacade;
    private CandidateTags newCandidateTag;
    private Integer activeIndex;
    private Country selectedCountry;
    private List<Country>countryList;
    private List<Province>provinceList;
    private Map<Country,List<Province>>mapCountryProvinces;
    @Inject
    private LanguageLevelFacade languageLevelFacade;
    
    //Image fields
    private PrimeFacesBufferedImage chosenColorBustImage;
    private PrimeFacesBufferedImage chosenBustImage;
    private PrimeFacesBufferedImage chosenBodyImage;
    private CandidateEducationalInfo collectorEducationalInfo;
    private CandidateProfessionalInfo collectorProfessionalInfo;
    private CandidateLanguageInfo collectorLanguageInfo;
    private CandidateInterestArea collectorInterestArea;
    private CandidateRelocatingInfo collectorRelocatingInfo;
    //Show videos
    private CandidateLanguageInfo selectedCurrentVideo;
    private CandidateLanguageInfo selectedEditVideo;

    @Inject
    @EditedEntityEvent
    private Event<Candidate> updatedCandidateEvent;
    
    private UploadedFile originalImageFile;
    private PrimeFacesBufferedImage originalImage;
    private PrimeFacesBufferedImage originalImageResized;
    private PrimeFacesBufferedImage bustCroppedImage;
    private String bustCroppedImageName;
    private PrimeFacesBufferedImage bodyCroppedImage;
    private String bodyCroppedImageName;

    //Cambio a almacenar las imagenes temporales
    private String originalImageFileName;
    
    //Cropping fields
    private Integer x1;
    private Integer y1;
    private Integer x2;
    private Integer y2;
    private Integer cropWidth;
    private Integer cropHeight;

    private final Integer maxBustSize=350;
    private final Integer maxBodySize=500;

    //Book images
    @Inject
    CandidateBookImageFacade bookImageEJB;
    private CandidateBookImage selectedPicture;

    @Inject
    private FacesMessageHelper messagesHelper;

    
    //@PostConstruct
    public String fillCandidateBean(Candidate candidate){
        this.setCandidate(candidate);
        adminCandidateEvent.fire(candidate);
        
        if(getCandidate().getCandidateEducationalInfoList()==null){
            getCandidate().setCandidateEducationalInfoList(new ArrayList<CandidateEducationalInfo>());
        }
        if(getCandidate().getCandidateProfessionalInfoList()==null){
            getCandidate().setCandidateProfessionalInfoList(new ArrayList<CandidateProfessionalInfo>());
        }
        if(getCandidate().getCandidateLanguageInfoList()==null){
            getCandidate().setCandidateLanguageInfoList(new ArrayList<CandidateLanguageInfo>());
        }
        if(getCandidate().getCandidateInterestAreaList()==null){
            getCandidate().setCandidateInterestAreaList(new ArrayList<CandidateInterestArea>());
        }
        if(getCandidate().getCandidateRelocatingInfoList()==null){
            getCandidate().setCandidateRelocatingInfoList(new ArrayList<CandidateRelocatingInfo>());
        }
        collectorEducationalInfo=new CandidateEducationalInfo();
        collectorProfessionalInfo=new CandidateProfessionalInfo();
        collectorLanguageInfo=new CandidateLanguageInfo();
        collectorInterestArea=new CandidateInterestArea();
        collectorRelocatingInfo=new CandidateRelocatingInfo();
        newCandidateTag= new CandidateTags();
        setActiveIndex((Integer) 0);
        countryList=new ArrayList<Country>();
        provinceList=new ArrayList <Province>();
        mapCountryProvinces=new HashMap<Country, List<Province>>();
        for(CandidateRelocatingInfo info:getCandidate().getCandidateRelocatingInfoList()){
            Province province = info.getProvince();
            Country country= info.getCountry();
            if(info!=null){
                if(province!=null){
                    if(mapCountryProvinces.containsKey(country)){
                        List<Province>aux=mapCountryProvinces.get(country);
                        if(!aux.contains(province)){
                            mapCountryProvinces.remove(country);
                            aux.add(province);
                            mapCountryProvinces.put(country, aux);
                        }
                    }
                    else{
                        List<Province>aux= new ArrayList<Province>();
                        aux.add(province);
                        mapCountryProvinces.put(country, aux);
                    }
                }
                if(!countryList.contains(country)){
                    countryList.add(country);
                }
            }
        }
        reorderProfessionalInfo();
                
        return "AdminEditCandidateView?faces-redirect=true";
    }
    

    public void addRelocatingInfo(){
        List<CandidateRelocatingInfo> selectedCandidateRelocatingInfo= new ArrayList<CandidateRelocatingInfo>();
        setSelectedCountry(null);
        for(Country country: countryList){
            if(!mapCountryProvinces.containsKey(country)){
                mapCountryProvinces.put(country, new ArrayList<Province>());
            }
        }
        for(Country country:mapCountryProvinces.keySet()){
            if(mapCountryProvinces.get(country).isEmpty()){
                CandidateRelocatingInfo candidateRI=new CandidateRelocatingInfo();
                candidateRI.setCandidate(candidate);
                candidateRI.setCountry(country);
                candidateRI.setProvince(null);
                selectedCandidateRelocatingInfo.add(candidateRI);
            }
            else{
                for(Province province: mapCountryProvinces.get(country)){
                    CandidateRelocatingInfo candidateRI=new CandidateRelocatingInfo();
                    candidateRI.setCandidate(candidate);
                    candidateRI.setCountry(country);
                    candidateRI.setProvince(province);
                    selectedCandidateRelocatingInfo.add(candidateRI);
                }
            }
        }
        for(Iterator<CandidateRelocatingInfo> iter=getCandidate().getCandidateRelocatingInfoList().iterator();iter.hasNext();){
                CandidateRelocatingInfo old=iter.next();
                if(selectedCandidateRelocatingInfo.contains(old)){
                    selectedCandidateRelocatingInfo.remove(old);
                }
                else{
                    iter.remove();
                }
        }
        for(Iterator<CandidateRelocatingInfo> iter=selectedCandidateRelocatingInfo.iterator();iter.hasNext();){
            CandidateRelocatingInfo old=iter.next();
            candidate.getCandidateRelocatingInfoList().add(old);
            iter.remove();
        }
        editAndShowResponse();
    }
    public void addCountry(SelectEvent event){
        Country country= (Country)event.getObject();
        if(!mapCountryProvinces.containsKey(country)){
            List<Province> provinces=new ArrayList<Province>();
            mapCountryProvinces.put(country, provinces);
        }
        setSelectedCountry(null);
    }
    public void addProvince(){
        if(!countryList.contains(selectedCountry)){
            countryList.add(selectedCountry);
        }
    }
    
    public void removeCountry(UnselectEvent event){
        Country countryToRemove= (Country)event.getObject();
        if(selectedCountry!=null && selectedCountry.equals(countryToRemove)){
           provinceList=new ArrayList<Province>();
       }
        mapCountryProvinces.remove(countryToRemove);
        setSelectedCountry(null);
    }
    
    public Boolean getAllProvinces(){
        Boolean res = true;
        if(provinceList!=null && !provinceList.isEmpty()){
            res =false;
        }
        return res;
    }
    
    public void setAllProvinces(Boolean seleccionado){
        provinceList=new ArrayList<Province>();
    }
    public void reinitEditCandidateBean(){
        chosenBodyImage=null;
        chosenBustImage=null;
        chosenColorBustImage=null;
        collectorEducationalInfo=new CandidateEducationalInfo();
        collectorProfessionalInfo=new CandidateProfessionalInfo();
        collectorLanguageInfo=new CandidateLanguageInfo();
        collectorInterestArea=new CandidateInterestArea();
        collectorRelocatingInfo=new CandidateRelocatingInfo();
    }
    
    public String reinitCollectorEducationalInfo() { 
        collectorEducationalInfo = new CandidateEducationalInfo();  
        return null;  
    }
    
    public void saveEducationalInfo(CandidateEducationalInfo item){
        
    }
    
    public String reinitCollectorProfessionalInfo() { 
        collectorProfessionalInfo=new CandidateProfessionalInfo();
        return null;  
    }
    
    public String reinitCollectorLanguageInfo() { 
        collectorLanguageInfo=new CandidateLanguageInfo(); 
        return null;  
    }
    
    public String reinitCollectorInterestArea() { 
        collectorInterestArea=new CandidateInterestArea(); 
        return null;  
    }
    
    public String reinitCollectorRelocatingInfo() { 
        collectorRelocatingInfo=new CandidateRelocatingInfo(); 
        return null;  
    }

    
    public void editAndShowResponse(){
        Date date = new Date();
        getCandidate().getUser().setLastDate(date);
        getCandidate().setLastModificationDate(date);
        candidateEJB.edit(getCandidate());
        messagesHelper.printMessage("messages", "UpdatedSuccessfully", FacesMessage.SEVERITY_INFO);
        updatedCandidateEvent.fire(candidate);
    }
    
    @Transactional
    public void editImages(){
        try{
            if(chosenColorBustImage!=null){
                getCandidate().setBustColor(ImageStoreHelper.storePrimeFacesBufferedImage(chosenColorBustImage));                
            }
            if(chosenBustImage!=null){
                getCandidate().setBustBlackWhite(ImageStoreHelper.storePrimeFacesBufferedImage(chosenBustImage));
            }
            if(chosenBodyImage!=null){
                getCandidate().setBodyFormal(ImageStoreHelper.storePrimeFacesBufferedImage(chosenBodyImage));
            }
            if(chosenColorBustImage!=null && chosenBustImage!=null && chosenBodyImage!=null){
                updateImages();
            }
            reinitEditImageBean();
            editAndShowResponse();
        }
        catch(FileNotFoundException fne){
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        } catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
        
    }
    
    public void editPersonal(){
        editAndShowResponse();
    }
    
    public void saveProfessional(){
        collectorProfessionalInfo.setCandidate(getCandidate());
        if(getCandidate().getCandidateProfessionalInfoList().contains(collectorProfessionalInfo)){
            for(Iterator<CandidateProfessionalInfo> iter=getCandidate().getCandidateProfessionalInfoList().iterator();iter.hasNext();){
                CandidateProfessionalInfo old=iter.next();
                if(old.equals(collectorProfessionalInfo)){
                    iter.remove();
                }
            }
        }
        getCandidate().getCandidateProfessionalInfoList().add(collectorProfessionalInfo);
        reorderProfessionalInfo();
        editAndShowResponse();
        collectorProfessionalInfo=new CandidateProfessionalInfo();
    }
    
    public void editInterest(){
        editAndShowResponse();
        collectorInterestArea=new CandidateInterestArea();
    }
    
    public void editLanguage(){
        editAndShowResponse();
        collectorLanguageInfo=new CandidateLanguageInfo();
    }
     
    public void editProfessional(){
        editAndShowResponse();
        reorderProfessionalInfo();
        collectorProfessionalInfo=new CandidateProfessionalInfo();
    }
     
     public void editEducational(){
        if(collectorEducationalInfo.getEducationalLevel().getIdEducationalLevel()<=2){
            collectorEducationalInfo.setDescription(null);
            collectorEducationalInfo.setSector(null);
            collectorEducationalInfo.setStatus(null);
        }
        editAndShowResponse();
        collectorEducationalInfo=new CandidateEducationalInfo();
    }
    
    public void saveEducational(){
        collectorEducationalInfo.setCandidate(getCandidate());
        if(getCandidate().getCandidateEducationalInfoList().contains(collectorEducationalInfo)){
            for(Iterator<CandidateEducationalInfo> iter=getCandidate().getCandidateEducationalInfoList().iterator();iter.hasNext();){
                CandidateEducationalInfo old=iter.next();
                if(old.equals(collectorEducationalInfo)){
                    iter.remove();
                }
            }
        }
        getCandidate().getCandidateEducationalInfoList().add(collectorEducationalInfo);
        editAndShowResponse();
        collectorEducationalInfo=new CandidateEducationalInfo();
    }
    
    public void saveLanguage() throws FileNotFoundException, IOException{
        collectorLanguageInfo.setCandidate(getCandidate());
        if(getCandidate().getCandidateLanguageInfoList().contains(collectorLanguageInfo)){
            for(Iterator<CandidateLanguageInfo> iter=getCandidate().getCandidateLanguageInfoList().iterator();iter.hasNext();){
                CandidateLanguageInfo old=iter.next();
                if(old.equals(collectorLanguageInfo)){
                    iter.remove();
                }
            }

        }
        getCandidate().getCandidateLanguageInfoList().add(collectorLanguageInfo);
        editAndShowResponse();
        collectorLanguageInfo=new CandidateLanguageInfo();
    }
    
    public void saveInterest(){
        collectorInterestArea.setCandidate(getCandidate());
        if(getCandidate().getCandidateInterestAreaList().contains(collectorInterestArea)){
            for(Iterator<CandidateInterestArea> iter=getCandidate().getCandidateInterestAreaList().iterator();iter.hasNext();){
                CandidateInterestArea old=iter.next();
                if(old.equals(collectorInterestArea)){
                    //candidateInterestAreaEJB.remove(old);
                    iter.remove();
                }
            }
        }
        getCandidate().getCandidateInterestAreaList().add(collectorInterestArea);
        editAndShowResponse();
        collectorInterestArea=new CandidateInterestArea();
    }
    
    public void updateImages() throws FileNotFoundException, IOException{
        File colorBustFile=new File(ImageStoreHelper.pathToStoringFolder+getCandidate().getBustColor());
        File newColorBustFile=new File(ImageStoreHelper.pathToPersistentStore+"/"+getCandidate().getIdCandidate().toString()+"/"+"images/"+getCandidate().getBustColor());
        File bwBustFile=new File(ImageStoreHelper.pathToStoringFolder+getCandidate().getBustBlackWhite());
        File newBWBustTile=new File(ImageStoreHelper.pathToPersistentStore+"/"+getCandidate().getIdCandidate().toString()+"/"+"images/"+getCandidate().getBustBlackWhite());
        File bodyFormalFile=new File(ImageStoreHelper.pathToStoringFolder+getCandidate().getBodyFormal());
        File newBodyFormalFile=new File(ImageStoreHelper.pathToPersistentStore+"/"+getCandidate().getIdCandidate().toString()+"/"+"images/"+getCandidate().getBodyFormal());
        ImageStoreHelper.copyAndDelete(colorBustFile, newColorBustFile);
        ImageStoreHelper.copyAndDelete(bodyFormalFile, newBodyFormalFile);
        ImageStoreHelper.copyAndDelete(bwBustFile, newBWBustTile);
    }
    
    public void createdCandidateTag() throws Exception{
        boolean res=candidateTagsFacade.candidateHasThisTag(candidate, newCandidateTag.getTag());
        if(!res &&candidate.getCandidateTagsList().size()<40){
            newCandidateTag.setCandidate(candidate);
            newCandidateTag.setTag(newCandidateTag.getTag().toLowerCase());
            candidateTagsFacade.create(newCandidateTag);
            candidate.getCandidateTagsList().add(newCandidateTag);
        }
        if(candidate.getCandidateTagsList().size()>39){    
            messagesHelper.printMessage("messages", "LimitSkillsReached", FacesMessage.SEVERITY_ERROR);
        }
        else{
            editAndShowResponse();
        }
        newCandidateTag= new CandidateTags();
    }
    
    public void removeCandidateTag(CandidateTags  candidateTags){
        candidate.getCandidateTagsList().remove(candidateTags);
        editAndShowResponse();
    }
    
    public List<String>autocompleteTags(String query){
        List<String>res= candidateTagsFacade.similarTags(query.toLowerCase());
        return res;
    }
    
    public void setSelectecAutocompleteItem(SelectEvent event) {
        String string =(String) event.getObject();
        newCandidateTag.setTag(string);
    }
    
    public void removeCandidateEducationalInfo(CandidateEducationalInfo educInfo){
        candidate.getCandidateEducationalInfoList().remove(educInfo);
        collectorEducationalInfo=new CandidateEducationalInfo();
        editAndShowResponse();
    }
    
    public void removeCandidateProfessionalInfo(CandidateProfessionalInfo profInfo){
        candidate.getCandidateProfessionalInfoList().remove(profInfo);
        collectorProfessionalInfo=new CandidateProfessionalInfo();
        reorderProfessionalInfo();
        editAndShowResponse();
    }
    
    public void removeCandidateLanguageInfo(CandidateLanguageInfo langInfo){
        collectorLanguageInfo=new CandidateLanguageInfo();
        candidate.getCandidateLanguageInfoList().remove(langInfo);
        editAndShowResponse();
    }
    
    public void removeCandidateRelocatingInfo(CandidateRelocatingInfo relInfo){
        collectorRelocatingInfo=new CandidateRelocatingInfo();
        candidate.getCandidateRelocatingInfoList().remove(relInfo);
        editAndShowResponse();
    }
    
    public void removeCandidateInterestArea(CandidateInterestArea intArea){
        collectorInterestArea= new CandidateInterestArea();
        candidate.getCandidateInterestAreaList().remove(intArea);
        editAndShowResponse();
    }
    
    public void reditectToProfessionalPanel() throws IOException{
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#professionalForm:professionalPanel");
    }
        
    
    public void cancelReditectToProfessionalPanel() throws IOException{
        if(collectorProfessionalInfo.getIdCandidateProfessionalInfo()!=null){
            collectorProfessionalInfo=candidateProfessionalInfoEJB.find(collectorProfessionalInfo.getIdCandidateProfessionalInfo());
            Collections.replaceAll(candidate.getCandidateProfessionalInfoList(), collectorProfessionalInfo, collectorProfessionalInfo);
        }
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#professionalForm:professionalPanel");
    }
            
    
    public void reditectToEducationalPanel() throws IOException{
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#educationalForm:educationalPanel");
    }
        
    
    public void cancelReditectEducationalPanel() throws IOException{
        if(collectorEducationalInfo.getIdCandidateEducationalInfo()!=null){
            collectorEducationalInfo=candidateEducationalInfoEJB.find(collectorEducationalInfo.getIdCandidateEducationalInfo());
            Collections.replaceAll(candidate.getCandidateEducationalInfoList(), collectorEducationalInfo, collectorEducationalInfo);
        }
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#educationalForm:educationalPanel");
    }
    
    
    public void reditectToInterestPanel() throws IOException{
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
          FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#interestForm:interestParentPanel");
    }
        
    
    public void cancelReditectInterestPanel() throws IOException{
        if(collectorInterestArea.getIdCandidateInterestArea()!=null){
            collectorInterestArea=candidateInterestAreaEJB.find(collectorInterestArea.getIdCandidateInterestArea());
            Collections.replaceAll(candidate.getCandidateInterestAreaList(), collectorInterestArea, collectorInterestArea);
        }
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#interestForm:interestParentPanel");
    }
    
    public void reditectToLanguagePanel() throws IOException{
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#languageForm:languagePanel");
    }
        
    public void cancelReditectLanguagePanel() throws IOException{
        if(collectorLanguageInfo.getIdCandidateLanguageInfo()!=null){
            collectorLanguageInfo=candidateLanguageInfoEJB.find(collectorLanguageInfo.getIdCandidateLanguageInfo());
            Collections.replaceAll(candidate.getCandidateLanguageInfoList(), collectorLanguageInfo, collectorLanguageInfo);
        }
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#languageForm:languagePanel");
    }
    
    public void reditectToRelocatingPanel() throws IOException{
        HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect(origRequest.getContextPath()+"/views/candidate/NewEditCandidateView.xhtml#relocatingForm:relocatingPanel");
    }
    
    /**
     * @return the candidate
     */
    public Candidate getCandidate() {
        return candidate;
    }

    /**
     * @param candidate the candidate to set
     */
    public void setCandidate(Candidate candidate) {
        this.candidate = candidate;
    }

    /**
     * @return the chosenBustImage
     */
    public PrimeFacesBufferedImage getChosenBustImage() {
        return chosenBustImage;
    }

    /**
     * @param chosenBustImage the chosenBustImage to set
     */
    public void setChosenBustImage(PrimeFacesBufferedImage chosenBustImage) {
        this.chosenBustImage = chosenBustImage;
    }

    /**
     * @return the chosenBodyImage
     */
    public PrimeFacesBufferedImage getChosenBodyImage() {
        return chosenBodyImage;
    }

    /**
     * @param chosenBodyImage the chosenBodyImage to set
     */
    public void setChosenBodyImage(PrimeFacesBufferedImage chosenBodyImage) {
        this.chosenBodyImage = chosenBodyImage;
    }

    /**
     * @return the collectorEducationalInfo
     */
    public CandidateEducationalInfo getCollectorEducationalInfo() {
        return collectorEducationalInfo;
    }

    /**
     * @param collectorEducationalInfo the collectorEducationalInfo to set
     */
    public void setCollectorEducationalInfo(CandidateEducationalInfo collectorEducationalInfo) {
        this.collectorEducationalInfo = collectorEducationalInfo;
    }

    /**
     * @return the collectorProfessionalInfo
     */
    public CandidateProfessionalInfo getCollectorProfessionalInfo() {
        return collectorProfessionalInfo;
    }

    /**
     * @param collectorProfessionalInfo the collectorProfessionalInfo to set
     */
    
    
    public void setCollectorProfessionalInfo(CandidateProfessionalInfo collectorProfessionalInfo) {
        this.collectorProfessionalInfo = collectorProfessionalInfo;
    }

    /**
     * @return the collectorLanguageInfo
     */
    public CandidateLanguageInfo getCollectorLanguageInfo() {
        return collectorLanguageInfo;
    }

    /**
     * @param collectorLanguageInfo the collectorLanguageInfo to set
     */
    public void setCollectorLanguageInfo(CandidateLanguageInfo collectorLanguageInfo) {
        this.collectorLanguageInfo = collectorLanguageInfo;
    }

    /**
     * @return the collectorInterestArea
     */
    public CandidateInterestArea getCollectorInterestArea() {
        return collectorInterestArea;
    }

    /**
     * @param collectorInterestArea the collectorInterestArea to set
     */
    public void setCollectorInterestArea(CandidateInterestArea collectorInterestArea) {
        this.collectorInterestArea = collectorInterestArea;
    }

    /**
     * @return the collectorRelocatingInfo
     */
    public CandidateRelocatingInfo getCollectorRelocatingInfo() {
        return collectorRelocatingInfo;
    }

    /**
     * @param collectorRelocatingInfo the collectorRelocatingInfo to set
     */
    public void setCollectorRelocatingInfo(CandidateRelocatingInfo collectorRelocatingInfo) {
        this.collectorRelocatingInfo = collectorRelocatingInfo;
    }

    /**
     * @return the chosenColorBustImage
     */
    public PrimeFacesBufferedImage getChosenColorBustImage() {
        return chosenColorBustImage;
    }

    /**
     * @param chosenColorBustImage the chosenColorBustImage to set
     */
    public void setChosenColorBustImage(PrimeFacesBufferedImage chosenColorBustImage) {
        this.chosenColorBustImage = chosenColorBustImage;
    }

    /**
     * @return the selectedCurrentVideo
     */
    public CandidateLanguageInfo getSelectedCurrentVideo() {
        return selectedCurrentVideo;
    }

    /**
     * @param selectedCurrentVideo the selectedCurrentVideo to set
     */
    public void setSelectedCurrentVideo(CandidateLanguageInfo selectedCurrentVideo) {
        this.selectedCurrentVideo = selectedCurrentVideo;
    }

    /**
     * @return the selectedEditVideo
     */
    public CandidateLanguageInfo getSelectedEditVideo() {
        return selectedEditVideo;
    }
    
    @Transactional
    public void setPrincipalVideoToCandidate(FileUploadEvent event) throws RollbackException{
        CandidateLanguageInfo principalLanguage=getCandidateLanguageVideo();
        if(principalLanguage!=null){
            candidate.getCandidateLanguageInfoList().remove(principalLanguage);
        }
        principalLanguage=new CandidateLanguageInfo();
        principalLanguage.setVideo("0");
        principalLanguage.setLanguage(candidate.getUser().getUserConfiguration().getLanguage());
        LanguageLevel languageLevelNative=languageLevelFacade.find(4);
        principalLanguage.setLanguageLevel(languageLevelNative);
        principalLanguage.setWritingLevel(languageLevelNative);
        principalLanguage.setCandidate(candidate);
        getCandidate().getCandidateLanguageInfoList().add(principalLanguage);
        candidateEJB.edit(candidate);
        try {
            videoProcesingEjb.storeTemporaryVideo(event.getFile(),candidate,principalLanguage,candidateLanguageInfoEJB);
            messagesHelper.printMessage("messages", "VideoUploadSuccess", FacesMessage.SEVERITY_INFO);
        } 
        catch (FileNotFoundException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            throw new RollbackException("Error al guardar el vídeo en setPrincipalVideoToCandidate");
        }
        catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            throw new RollbackException("Error al guardar el vídeo en setPrincipalVideoToCandidate");
        }
        catch (NullPointerException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            throw new RollbackException("Error al guardar el vídeo en setPrincipalVideoToCandidate");
        }
    }
    
    @Transactional
    public void setVideoToCandidate(FileUploadEvent event) throws RollbackException{
        if(collectorLanguageInfo.getIdCandidateLanguageInfo()==null){
            if(getCandidate().getCandidateLanguageInfoList().contains(collectorLanguageInfo)){
                for(Iterator<CandidateLanguageInfo> iter=getCandidate().getCandidateLanguageInfoList().iterator();iter.hasNext();){
                    CandidateLanguageInfo old=iter.next();
                    if(old.equals(collectorLanguageInfo)){
                        candidateLanguageInfoEJB.remove(old);
                        iter.remove();
                    }
                }
            }
        }
        if(collectorLanguageInfo.getIdCandidateLanguageInfo()!=null){
            candidateLanguageInfoEJB.edit(collectorLanguageInfo);
        }
        else{
            collectorLanguageInfo.setCandidate(candidate);
            candidateLanguageInfoEJB.create(collectorLanguageInfo);
            getCandidate().getCandidateLanguageInfoList().add(collectorLanguageInfo);
        }
        collectorLanguageInfo.setVideo("0");
        try{
            videoProcesingEjb.storeTemporaryVideo(event.getFile(),candidate,collectorLanguageInfo,candidateLanguageInfoEJB);
            messagesHelper.printMessage("messages", "VideoUploadSuccess", FacesMessage.SEVERITY_INFO);
        }
        catch (FileNotFoundException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            throw new RollbackException("Error al guardar el vídeo en setPrincipalVideoToCandidate");
        }
        catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            throw new RollbackException("Error al guardar el vídeo en setPrincipalVideoToCandidate");
        }
        catch (NullPointerException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            throw new RollbackException("Error al guardar el vídeo en setPrincipalVideoToCandidate");
        }
    }

    /**
     * @return the newCandidateTag
     */
    public CandidateTags getNewCandidateTag() {
        return newCandidateTag;
    }

    /**
     * @param newCandidateTag the newCandidateTag to set
     */
    public void setNewCandidateTag(CandidateTags newCandidateTag) {
        this.newCandidateTag = newCandidateTag;
    }

    /**
     * @return the activeIndex
     */
    public Integer getActiveIndex() {
        return activeIndex;
    }

    /**
     * @param activeIndex the activeIndex to set
     */
    public void setActiveIndex(Integer activeIndex) {
        this.activeIndex = activeIndex;
    }

    /**
     * @return the selectedCountry
     */
    public Country getSelectedCountry() {
        return selectedCountry;
    }

    /**
     * @param selectedCountry the selectedCountry to set
     */
    public void setSelectedCountry(Country selectedCountry) {

        //ponemos en la lista de provincias las que ya tenemos de ese pais
        if(mapCountryProvinces.containsKey(selectedCountry)){
            provinceList=mapCountryProvinces.get(selectedCountry);
        }
        else{
            provinceList=new ArrayList<Province>();
        }
        this.selectedCountry = selectedCountry;
    }
    
    public void addProvinces(){
        if(mapCountryProvinces.containsKey(this.selectedCountry)){
                mapCountryProvinces.remove(this.selectedCountry);
        }
        mapCountryProvinces.put(this.selectedCountry, provinceList);
        
    }
    
    
    public void reinitEditImageBean(){
        x1=null;
        y1=null;
        x2=null;
        y2=null;
        cropWidth=null;
        cropHeight=null;
        originalImage=null;
        originalImageResized=null;
        bustCroppedImage=null;
        bodyCroppedImage=null;
        bodyCroppedImageName=null;
        chosenBustImage=null;
        chosenBodyImage=null;
        originalImageFileName=null;
    }
    
    public void processOriginalImageFile(FileUploadEvent event){ 
        try {
            originalImageFileName=null;
            originalImage=null;
            originalImageResized=null;
            bustCroppedImage=null;
            bustCroppedImageName=null;
            bodyCroppedImage=null;
            bodyCroppedImageName=null;
            chosenBustImage=null;
            chosenBodyImage=null;
            BufferedImageMaker imageMaker=new GeneralBufferedImageMaker();
            BufferedImage originalSizeImage=imageMaker.getBufferedImageFromInputStream(event.getFile().getInputstream());
            originalImage=new PrimeFacesBufferedImage(originalSizeImage);
            Integer maxPixels=800;
            originalImageResized=new PrimeFacesBufferedImage(this.getScaledImage(originalSizeImage,maxPixels));
            originalImageFileName=ImageStoreHelper.storePrimeFacesBufferedImage(originalImageResized);
        } 
        catch(FileNotFoundException fne){
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            originalImageResized=null;
            originalImageFileName=null;
        } 
        catch(IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            originalImageResized=null;
            originalImageFileName=null;
        }
        catch(NullPointerException nex){
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            originalImageResized=null;
            originalImageFileName=null;
        }
    }
    
    
    
    private BufferedImage getScaledImage(BufferedImage originalSizeImage, Integer maxPixels){
        Integer originalWidt=new Integer(originalSizeImage.getWidth());
        Integer originalHeigh=new Integer(originalSizeImage.getHeight());
        Double originalWidth=originalWidt.doubleValue();
        Double originalHeight=originalHeigh.doubleValue();
        Double ratio=originalWidth/originalHeight;
        Integer resWidth=null;
        Integer resHeight=null;
        if(ratio>1){
            resWidth=maxPixels;
            resHeight=(int)Math.ceil(maxPixels/ratio);
        }
        else{
           resHeight=maxPixels;
            resWidth=(int)Math.ceil(maxPixels*ratio); 
        }
        BufferedImage scaledImage = new BufferedImage(resWidth, resHeight, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(originalSizeImage, 0, 0, resWidth, resHeight, null);
        graphics2D.dispose();
        return scaledImage;
    }
    
    
    private BufferedImage getScaledImageRespectingFormat(BufferedImage originalSizeImage, Integer maxPixels){
        Integer originalWidt=new Integer(originalSizeImage.getWidth());
        Integer originalHeigh=new Integer(originalSizeImage.getHeight());
        Double originalWidth=originalWidt.doubleValue();
        Double originalHeight=originalHeigh.doubleValue();
        Double ratio=originalWidth/originalHeight;
        Integer resWidth=null;
        Integer resHeight=null;
        if(ratio>1){
            resWidth=maxPixels;
            resHeight=(int)Math.ceil(maxPixels/ratio);
        }
        else{
           resHeight=maxPixels;
            resWidth=(int)Math.ceil(maxPixels*ratio); 
        }
        BufferedImage scaledImage = new BufferedImage(resWidth, resHeight, originalSizeImage.getType());
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(originalSizeImage, 0, 0, resWidth, resHeight, null);
        graphics2D.dispose();
        return scaledImage;
    }
    
    public void rotateImage(){
        try {
            BufferedImage bufferedImage=originalImageResized.getBufferedImage();
            BufferedImage newImage = new BufferedImage( bufferedImage.getHeight(),bufferedImage.getWidth(), bufferedImage.getType());
            AffineTransform tx = new AffineTransform();
            // last, width = height and height = width
            tx.translate(bufferedImage.getHeight() / 2,bufferedImage.getWidth() / 2);
            tx.rotate(Math.toRadians(90));
            // first - center image at the origin so rotate works OK
            tx.translate(-bufferedImage.getWidth() / 2,-bufferedImage.getHeight() / 2);
            Graphics2D g2 = newImage.createGraphics();
            g2.drawImage(bufferedImage, tx, null);
            bufferedImage = newImage;
            g2.dispose();
            originalImageResized.setBufferedImage(bufferedImage);
            originalImageFileName=ImageStoreHelper.storePrimeFacesBufferedImage(originalImageResized);
        } catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    public void cropImageAndRedirect(){
        try {
            BufferedImage bufferedImage=originalImageResized.getBufferedImage();
            BufferedImage croppedImage=bufferedImage.getSubimage(x1, y1, cropWidth, cropHeight);
            BufferedImage colorResultImage=new BufferedImage(croppedImage.getWidth(), croppedImage.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
            BufferedImage resultImage = new BufferedImage(croppedImage.getWidth(), croppedImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
            Graphics g = resultImage.getGraphics();
            Graphics g2 = colorResultImage.getGraphics();
            g.drawImage(croppedImage, 0, 0, null);
            g2.drawImage(croppedImage, 0, 0, null);
            g.dispose();
            g2.dispose();
            bustCroppedImage=new PrimeFacesBufferedImage(getScaledImageRespectingFormat(colorResultImage, maxBustSize));
            PrimeFacesBufferedImage pbi=new PrimeFacesBufferedImage(getScaledImage(resultImage, maxBustSize));
            chosenBustImage=pbi;
            chosenColorBustImage=getBustCroppedImage();
            bustCroppedImageName=ImageStoreHelper.storePrimeFacesBufferedImage(chosenBustImage);
        } 
        catch (FileNotFoundException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            chosenBustImage=null;
            chosenColorBustImage=null;
            bustCroppedImageName=null;
        }
        catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            chosenBustImage=null;
            chosenColorBustImage=null;
            bustCroppedImageName=null;
        }
        catch (NullPointerException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
            chosenBustImage=null;
            chosenColorBustImage=null;
            bustCroppedImageName=null;
        }
    }
    
//out    public void onEditedUserConfigurtion(@Observes @EditedEntityEvent UserConfiguration userConfiguration){
//        
//        setCandidatePrincipalVideo(); 
//    }
    
    public void cropBodyImageAndRedirect(){
        try {
            BufferedImage bufferedImage=originalImageResized.getBufferedImage();
            BufferedImage croppedImage=bufferedImage.getSubimage(x1, y1, cropWidth, cropHeight);
            BufferedImage resultImage =  new BufferedImage(croppedImage.getWidth(),croppedImage.getHeight(), BufferedImage.TYPE_INT_RGB );
            Graphics g = resultImage.getGraphics();
            g.drawImage(croppedImage, 0, 0, null);
            PrimeFacesBufferedImage pbi=new PrimeFacesBufferedImage(getScaledImageRespectingFormat(resultImage, maxBodySize));
            bodyCroppedImage=pbi;
            chosenBodyImage=pbi;
            bodyCroppedImageName=ImageStoreHelper.storePrimeFacesBufferedImage(chosenBodyImage);
        } 
        catch (FileNotFoundException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
        catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
        catch (NullPointerException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    public void validateExperienceFinishDateDialog(ComponentSystemEvent event) {
        validateExperienceFinishDate(event,"editFinishDateProfessional","editStartDateProfessional","workingNowDialog");
    }
    
    public void validateExperienceFinishDatePanel(ComponentSystemEvent event) {
        validateExperienceFinishDate(event,"finishDateProfessional","startDateProfessional","workingNow");
    }
    
    
    public void validateExperienceFinishDate(ComponentSystemEvent event,String finisDateComponent, String startDateComponent, String workingNowComponent){
        UIComponent components = event.getComponent();
	UIInput uiInputFinishDate = (UIInput) components.findComponent(finisDateComponent);
        Date dateFinish = (Date)uiInputFinishDate.getLocalValue();
        UIInput workingNow = (UIInput) components.findComponent(workingNowComponent);
	Boolean workingNowBolean = (Boolean)workingNow.getLocalValue();
        UIInput uiInputstartDate = (UIInput) components.findComponent(startDateComponent);
	Date dateStart = (Date)uiInputstartDate.getLocalValue();
        if(workingNowBolean==false && dateFinish==null){
            messagesHelper.printMessage("messages", "NoFinishOrCurrentProfessionalDate", FacesMessage.SEVERITY_ERROR);
        }
        else if(workingNowBolean==true && dateFinish!=null){
            messagesHelper.printMessage("messages", "FinishAndCurrentProfessionalDate", FacesMessage.SEVERITY_ERROR);
        }
        else if(workingNowBolean==false && dateFinish!=null && dateStart!=null && dateFinish.compareTo(dateStart)<0){
            messagesHelper.printMessage("messages", "StartDateAfterFinishDate", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    @Transactional
    public void uploadBookImage(FileUploadEvent event){ 
        try{
            CandidateBookImage image=new CandidateBookImage();
            BufferedImageMaker imageMaker=new GeneralBufferedImageMaker();
            BufferedImage originalSizeImage=imageMaker.getBufferedImageFromInputStream(event.getFile().getInputstream());
            Integer maxPixels=800;
            PrimeFacesBufferedImage bookOriginalImageResized=new PrimeFacesBufferedImage(this.getScaledImage(originalSizeImage,maxPixels));
            String fileName=ImageStoreHelper.storePersistentlyPrimeFacesBufferedImage(bookOriginalImageResized, getCandidate());
            image.setCandidate(getCandidate());
            image.setImage(fileName);
            bookImageEJB.create(image);
            candidate.getCandidateBookImageList().add(image);
        }
        catch (FileNotFoundException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
        catch (IOException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
        catch (NullPointerException ex) {
            messagesHelper.printMessage("messages", "IOExceptionMessage", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    public void removeImage(){
        candidate.getCandidateBookImageList().remove(selectedPicture);
        bookImageEJB.remove(selectedPicture);
    }
     
    public void reorderProfessionalInfo(){
        Collections.sort(getCandidate().getCandidateProfessionalInfoList());
    }
    
    public void removeProfileImages(){
        candidate.setBustColor(null);
        candidate.setBustBlackWhite(null);
        candidate.setBodyFormal(null);
        candidateEJB.edit(candidate);
    }
    
    /**
     * @return the originalImage
     */
    public PrimeFacesBufferedImage getOriginalImage() {
        return originalImage;
    }

    /**
     * @param originalImage the originalImage to set
     */
    public void setOriginalImage(PrimeFacesBufferedImage originalImage) {
        this.originalImage = originalImage;
    }

    /**
     * @return the originalImageResized
     */
    public PrimeFacesBufferedImage getOriginalImageResized() {
        return originalImageResized;
    }

    /**
     * @param originalImageResized the originalImageResized to set
     */
    public void setOriginalImageResized(PrimeFacesBufferedImage originalImageResized) {
        this.originalImageResized = originalImageResized;
    }
    /**
     * @return the originalImageFile
     */
    public UploadedFile getOriginalImageFile() {
        return originalImageFile;
    }

    /**
     * @param originalImageFile the originalImageFile to set
     */
    public void setOriginalImageFile(UploadedFile originalImageFile) {
        this.originalImageFile = originalImageFile;
    }

    /**
     * @return the x1
     */
    public Integer getX1() {
        return x1;
    }

    /**
     * @param x1 the x1 to set
     */
    public void setX1(Integer x1) {
        this.x1 = x1;
    }

    /**
     * @return the y1
     */
    public Integer getY1() {
        return y1;
    }

    /**
     * @param y1 the y1 to set
     */
    public void setY1(Integer y1) {
        this.y1 = y1;
    }

    /**
     * @return the x2
     */
    public Integer getX2() {
        return x2;
    }

    /**
     * @param x2 the x2 to set
     */
    public void setX2(Integer x2) {
        this.x2 = x2;
    }

    /**
     * @return the y2
     */
    public Integer getY2() {
        return y2;
    }

    /**
     * @param y2 the y2 to set
     */
    public void setY2(Integer y2) {
        this.y2 = y2;
    }

    /**
     * @return the cropWidth
     */
    public Integer getCropWidth() {
        return cropWidth;
    }

    /**
     * @param cropWidth the cropWidth to set
     */
    public void setCropWidth(Integer cropWidth) {
        this.cropWidth = cropWidth;
    }

    /**
     * @return the cropHeight
     */
    public Integer getCropHeight() {
        return cropHeight;
    }

    /**
     * @param cropHeight the cropHeight to set
     */
    public void setCropHeight(Integer cropHeight) {
        this.cropHeight = cropHeight;
    }

    /**
     * @return the bodyCroppedImage
     */
    public PrimeFacesBufferedImage getBodyCroppedImage() {
        return bodyCroppedImage;
    }

    /**
     * @param bodyCroppedImage the bodyCroppedImage to set
     */
    public void setBodyCroppedImage(PrimeFacesBufferedImage bodyCroppedImage) {
        this.bodyCroppedImage = bodyCroppedImage;
    }

    /**
     * @return the bustCroppedImage
     */
    public PrimeFacesBufferedImage getBustCroppedImage() {
        return bustCroppedImage;
    }

    /**
     * @param bustCroppedImage the bustCroppedImage to set
     */
    public void setBustCroppedImage(PrimeFacesBufferedImage bustCroppedImage) {
        this.bustCroppedImage = bustCroppedImage;
    }

    /**
     * @return the originalImageFileName
     */
    public String getOriginalImageFileName() {
        return originalImageFileName;
    }

    /**
     * @param originalImageFileName the originalImageFileName to set
     */
    public void setOriginalImageFileName(String originalImageFileName) {
        this.originalImageFileName = originalImageFileName;
    }

    /**
     * @return the bustCroppedImageName
     */
    public String getBustCroppedImageName() {
        return bustCroppedImageName;
    }

    /**
     * @param bustCroppedImageName the bustCroppedImageName to set
     */
    public void setBustCroppedImageName(String bustCroppedImageName) {
        this.bustCroppedImageName = bustCroppedImageName;
    }

    /**
     * @return the bodyCroppedImageName
     */
    public String getBodyCroppedImageName() {
        return bodyCroppedImageName;
    }

    /**
     * @param bodyCroppedImageName the bodyCroppedImageName to set
     */
    public void setBodyCroppedImageName(String bodyCroppedImageName) {
        this.bodyCroppedImageName = bodyCroppedImageName;
    }


    /**
     * @return the countryList
     */
    public List<Country> getCountryList() {
        return countryList;
    }

    /**
     * @param countryList the countryList to set
     */
    public void setCountryList(List<Country> countryList) {
        this.countryList = countryList;
    }

    /**
     * @return the provinceList
     */
    public List<Province> getProvinceList() {
        return provinceList;
    }

    /**
     * @param provinceList the provinceList to set
     */
    public void setProvinceList(List<Province> provinceList) {
            this.provinceList = provinceList;
    }

    /**
     * @return the candidateLanguageVideo
     */
    public CandidateLanguageInfo getCandidateLanguageVideo() {
        CandidateLanguageInfo principal=null;
        for(CandidateLanguageInfo candidateLanguageInfo:candidate.getCandidateLanguageInfoList()){
            if(candidateLanguageInfo.getLanguage().equals(candidate.getUser().getUserConfiguration().getLanguage())){
                principal=candidateLanguageInfo;
                break;
            }
        }
        return principal;
    }
    
    /**
     * @return the selectedPicture
     */
    public CandidateBookImage getSelectedPicture() {
        return selectedPicture;
    }

    /**
     * @param selectedPicture the selectedPicture to set
     */
    public void setSelectedPicture(CandidateBookImage selectedPicture) {
        this.selectedPicture = selectedPicture;
    }        
}
